import dayjs from 'dayjs'

export function formatTime(date, pattern) {
  return dayjs(date).format(pattern)
}

export function treeToList(tree = [], idValue = null, childrenField = 'children', idField = 'id', parentIdField =
  'parentId') {
  const list = []
  if (!childrenField) childrenField = 'children'
  for (let i = 0, j = tree.length; i < j; i++) {
    const d = tree[i]
    const id = d[idField]
    if (!list.some(l => l[idField] === id)) {
      list.push(d)
    }
    if (parentIdField) d[parentIdField] = idValue
    const children = d[childrenField]
    if (children && children.length > 0) {
      const items = treeToList(children, id, childrenField, idField, parentIdField)
      const values = items.values()
      for (const v of values) {
        if (!list.some(l => l[idField] === v[idField])) {
          list.push(v)
        }
      }
    }
  }
  return list
}

export function listToTree(list = [], root = null, idField = 'id', parentIdField = 'parentId') {
  const tree = []
  const hash = {}
  const childrenField = 'children'
  //建立id与对象hash映射
  for (let i = 0, l = list.length; i < l; i++) {
    const d = list[i]
    hash[d[idField]] = d
  }

  for (let i = 0, l = list.length; i < l; i++) {
    const d = list[i]
    const parentID = d[parentIdField]

    //构建根节点
    if (parentID === '' || parentID === '00000000-0000-0000-0000-000000000000') {
      tree.push(d)
      continue
    }

    const parent = hash[parentID]
    if (!parent) {
      tree.push(d)
      continue
    }
    //构建子节点
    let children = parent[childrenField]
    if (!children) {
      children = []
      parent[childrenField] = children
    }
    children.push(d)
  }

  //若根节点不为空，则添加根节点
  if (root) {
    root[childrenField] = tree
    return [root]
  }

  return tree
}

export function getListParents(list = [], idValue, idField = 'id', parentIdField = 'parentId', includeSelf = false) {
  const parents = []
  const self = list.find(o => o[idField] === idValue)
  if (!self) {
    return parents
  }

  if (includeSelf) {
    parents.unshift(self)
  }

  let parent = list.find(o => o[idField] === self[parentIdField])
  while (parent && parent[idField] !== '' && parent[idField] !== '00000000-0000-0000-0000-000000000000') {
    parents.unshift(parent)
    parent = list.find(o => o[idField] === parent[parentIdField])
  }
  return parents
}

export function getTreeParents(tree = [], idValue, childrenField = 'children', idField = 'id', parentIdField =
  'parentId', parentIdValue = '00000000-0000-0000-0000-000000000000') {
  const list = treeToList(tree, parentIdValue, childrenField, idField, parentIdField)
  return getListParents(list, idValue, idField, parentIdField)
}

export function getTreeParentsWithSelf(tree = [], idValue, childrenField = 'children', idField = 'id', parentIdField =
  'parentId', parentIdValue = '00000000-0000-0000-0000-000000000000') {
  const list = treeToList(tree, parentIdValue, childrenField, idField, parentIdField)
  return getListParents(list, idValue, idField, parentIdField, true)
}

export function printHTML(id) {
  const html = document.querySelector('#' + id).innerHTML

  // 新建一个 DOM
  const div = document.createElement('div')
  const printDOMID = 'printDOMElement'
  div.id = printDOMID
  div.innerHTML = html

  // 提取第一个表格的内容 即表头
  const ths = div.querySelectorAll('.el-table__header-wrapper th')
  const ThsTextArry = []
  for (let i = 0, len = ths.length; i < len; i++) {
    if (ths[i].innerText !== '') ThsTextArry.push(ths[i].innerText)
  }

  // 删除多余的表头
  div.querySelector('.hidden-columns').remove()
  // 第一个表格的内容提取出来后已经没用了 删掉
  div.querySelector('.el-table__header-wrapper').remove()

  // 将第一个表格的内容插入到第二个表格
  let newHTML = ''
  for (let i = 0, len = ThsTextArry.length; i < len; i++) {
    newHTML += '' + ThsTextArry[i] + ''
  }
  newHTML += ''
  div.querySelector('.el-table__body-wrapper table').insertAdjacentHTML('afterbegin', newHTML)

  // 将新的 DIV 添加到页面 打印后再删掉
  document.querySelector('body').appendChild(div)
  printJS({
    printable: printDOMID,
    type: 'html',
    scanStyles: false,
    style: 'table { border-collapse: collapse }' // 表格样式
  })
  div.remove()
}

//保持输出顺序与表头一致
export function formatJson(filterVal, jsonData) {
  return jsonData.map((v) => filterVal.map((j) => v[j]));
}